package skeleton;

import java.util.ArrayList;


public class Generator extends Switch {

    private ArrayList<Sign> pattern;

    private int patternIndex;

    public Generator (String inPattern) {
        Logger.Message("Generator_konstruktor args: pattern " + pattern);
        
        //Alap állapot
        patternIndex = 0;
        
        //Check valid pattern
        if (inPattern.equals("")) ErrorController.signalError("Empty generator pattern!");
        
        for(int i = 0; i < inPattern.length(); i++){
            if( inPattern.charAt(i) != '0' ){
                if (inPattern.charAt(i) != '1')
                    ErrorController.signalError("Invalid generator pattern!");
            }
        }

        pattern = new ArrayList<Sign>();

        for(int i = 0; i < inPattern.length(); i++){
            if(inPattern.charAt(i) == String.valueOf(1).charAt(0)){
                this.pattern.add(Sign.High);
            }
            else{
                this.pattern.add(Sign.Low);
            }
        }
    }

    @Override
    public void stepState () {
        Logger.Message("Generator_stepState");
        
        //pattern leptetese
        if(patternIndex == pattern.size() - 1)
            patternIndex = 0;
        else patternIndex++;
        
        //wirek updatelese
        for(Wire wire : outputList){
            wire.setDigitalValue(pattern.get(patternIndex));
        }
    }

    public void reset () {
        Logger.Message("Generator_reset");
        
        patternIndex = 0;
    }

}

